class BluetoothManager(private val context: Context) {
    private var bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
    private var socket: BluetoothSocket? = null
    
    fun isBluetoothEnabled(): Boolean {
        return bluetoothAdapter?.isEnabled == true
    }

    fun startDiscovery(onDeviceFound: (BluetoothDevice) -> Unit) {
        if (!isBluetoothEnabled()) return

        val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
        context.registerReceiver(object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                when(intent?.action) {
                    BluetoothDevice.ACTION_FOUND -> {
                        val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
                        device?.let { onDeviceFound(it) }
                    }
                }
            }
        }, filter)

        bluetoothAdapter?.startDiscovery()
    }

    fun connectToDevice(device: BluetoothDevice, onConnected: () -> Unit, onError: (String) -> Unit) {
        try {
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString("YOUR-UUID-HERE"))
            socket?.connect()
            onConnected()
        } catch (e: Exception) {
            onError(e.message ?: "连接失败")
        }
    }
} 